<?php

namespace App\Http\Controllers\Manage;

use App\Http\Controllers\ApiController;
use App\Models\CnDiseaseDictionary;
use App\Models\DiseaseDictionary;
use App\Project\Traits\PinYinSearchAble;
use App\Project\Traits\SearchAble;
use App\Transformers\DiseaseDictionaryTransformer;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

/**
 * 诊断字典控制器
 * Class DiseaseController
 * @package App\Http\Controllers\Manage
 */
class DiseaseController extends ApiController
{
    use PinYinSearchAble;

    protected $searchQueryMap = [
        'name' => ['field' => 'name', 'operator' => 'like'],
    ];

    public function index(Request $request)
    {
        return $this->disease($request, new DiseaseDictionary());
    }

    public function cnType()
    {
        return $this->response(app('field')->get('diagnose.cn_diagnose.type'));
    }

    public function cnDisease(Request $request)
    {
        return $this->disease($request, new CnDiseaseDictionary());
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    protected function disease(Request $request, Model $model)
    {
        $size = $request->get('size') ?: 20;

        $model = $this->pinYinSearch($request->input(), $model);

        $data = $model->paginate($size)->appends($request->input());

        return $this->paginate($data, new DiseaseDictionaryTransformer());
    }
}
